package org.trackcore.handler;

import org.trackcore.model.entity.PerformanceXHR;
import org.trackcore.service.PerformanceXHRService;
import org.trackcore.utils.DataUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class XHRHandler implements DataHandler {
    private final PerformanceXHRService service;

    public XHRHandler(PerformanceXHRService service) {
        this.service = service;
    }

    @Override
    public String getHandlerKey() {
        return "performance_xhr"; // 对应前端subType
    }

    @Override
    public void handle(Map<String, Object> data, String logId) {
        PerformanceXHR xhr = PerformanceXHR.builder()
                .logId(logId)
                .url(DataUtils.getString(data, "url"))
                .method(DataUtils.getString(data, "method", "GET"))
                .status(DataUtils.getInt(data, "status"))
                .duration(DataUtils.getDouble(data, "duration"))
                .startTime(DataUtils.getDouble(data, "startTime"))
                .endTime(DataUtils.getDouble(data, "endTime"))
                .success(DataUtils.getBoolean(data, "success"))
                .build();

        service.save(xhr);
    }
}